home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / STATS_T.C < prev    next >
C/C++ Source or Header  |  1997-07-30  |  9KB  |  323 lines

  1. #include "global.h"
  2. #ifdef STATS_TFC
  3. #include "stats.h"
  4. #include "stats_t.h"
  5.  
  6.  
  7. #if !defined(_lint)
  8. static char rcsid[] OPTIONAL = "$Id: stats_t.c,v 1.10 1997/07/31 00:44:20 root Exp root $";
  9. #endif
  10.  
  11. static struct tfc_stats tfcstats;
  12.  
  13. static const char tfcdailyhdr[] = "%8.8s%-35.35sPERSONAL\n\n";
  14. static const char tfcdailyhdr2[] = "%10.10s%-32.32sFORWARDED\n\n";
  15. static const char tfcweeklyhdr[] = "%12.12s%-41.41sPERSONAL\n\n";
  16. static const char tfcweeklyhdr2[] = "%15.15s%-37.37sFORWARDED\n\n";
  17. static const char *usagestr = "Traffic";
  18. static const char trafficstr[] = "TRAFFIC";
  19. static const char bulletinstr[] = "BULLETINS";
  20. static const char ntsstr[] = "NTS";
  21. static const char tfcmonthlyhdr[] = "%11.11s%-43.43sPERSONAL\n\n";
  22. static const char tfcmonthlyhdr2[] = "%14.14s%-39.39sFORWARDED\n\n";
  23. static const char tfcyearlyhdr[] = "%2.2s%-23.23sPERSONAL\n\n";
  24. static const char tfcyearlyhdr2[] = "%4.4s%-21.21sFORWARDED\n\n";
  25.  
  26.  
  27. static struct cmds STATStrafficcmds[] = {
  28.     { "clear",    doSTATclear_tfc,0,    0,    NULLCHAR },
  29.     { "daily",    STATdaily_tfc,    0,    0,    NULLCHAR },
  30.     { "general",    STATgeneral_tfc,0,     0,    NULLCHAR },
  31.     { "monthly",    STATmonthly_tfc,0,     0,    NULLCHAR },
  32.     { "weekly",    STATweekly_tfc,    0,     0,    NULLCHAR },
  33.     { "yearly",    STATyearly_tfc,    0,     0,    NULLCHAR },
  34.     { NULLCHAR,    NULL,        0,    0,    NULLCHAR }
  35. };
  36.  
  37.  
  38.  
  39. int
  40. doSTATtraffic(argc,argv,p)
  41. int argc;
  42. char *argv[];
  43. void *p;
  44. {
  45.     return subcmd(STATStrafficcmds,argc,argv,p);
  46. }
  47.  
  48.  
  49. int
  50. doSTATclear_tfc(argc,argv,p)
  51. int argc OPTIONAL;
  52. char *argv[] OPTIONAL;
  53. void *p OPTIONAL;
  54. {
  55.     if(Curproc->input != Command->input)
  56.         tputs (STAT_cannotclear);
  57.     else    {
  58.         memset (&tfcstats, 0, sizeof (struct tfc_stats));
  59.         tfcstats.days = 1;
  60.         tfcstats.start = time((time_t *)0);
  61.         savestats_tfc();
  62.         log (-1, "Clearing traffic stats");
  63.     }
  64.     return 0;
  65. }
  66.  
  67.  
  68. int
  69. STATgeneral_tfc(argc,argv,p)
  70. int argc OPTIONAL;
  71. char *argv[] OPTIONAL;
  72. void *p OPTIONAL;
  73. {
  74. char buf1[26], buf2[26], buf3[36];
  75.  
  76.     tprintf ("\n%26.26s: %-26.26s%-24.24s\n", trafficstr, bulletinstr, "PERSONAL");
  77.     sprintf (buf1, "%ld", tfcstats.messages[0]);
  78.     sprintf (buf2, "%ld", tfcstats.messages[1]);
  79.     sprintf (buf3, "Traffic since %6.6s", &(ctime(&tfcstats.start))[4]);
  80.     tprintf ("  %24.24s: %-26.26s%-24.24s\n", buf3, buf1, buf2);
  81.     sprintf (buf1, "%ld", tfcstats.day[0]);
  82.     sprintf (buf2, "%ld", tfcstats.day[1]);
  83.     tprintf ("  %24.24s: %-26.26s%-24.24s\n", "Traffic since midnight",
  84.         buf1, buf2);
  85.     sprintf (buf1, "%ld", tfcstats.hour[0]);
  86.     sprintf (buf2, "%ld", tfcstats.hour[1]);
  87.     tprintf ("  %24.24s: %-26.26s%-24.24s\n", "Traffic this hour",
  88.         buf1, buf2);
  89.     sprintf (buf1, "%d", (tfcstats.days > 1) ? (int)((tfcstats.messages[0] - tfcstats.day[0] + (tfcstats.days - 2)) / (tfcstats.days - 1)) : (int)tfcstats.messages[0]);
  90.     sprintf (buf2, "%d", (tfcstats.days > 1) ? (int)((tfcstats.messages[1] - tfcstats.day[1] + (tfcstats.days - 2)) / (tfcstats.days - 1)) : (int)tfcstats.messages[1]);
  91.     tprintf ("  %24.24s: %-26.26s%-24.24s\n\n", "Average traffic per day",
  92.         buf1, buf2);
  93.  
  94.     tprintf ("\n%26.26s: %-26.26s%-24.24s\n", trafficstr, ntsstr, "FORWARDED");
  95.     sprintf (buf1, "%ld", tfcstats.messages2[0]);
  96.     sprintf (buf2, "%ld", tfcstats.messages2[1]);
  97.     sprintf (buf3, "Traffic since %6.6s", &(ctime(&tfcstats.start))[4]);
  98.     tprintf ("  %24.24s: %-26.26s%-24.24s\n", buf3, buf1, buf2);
  99.     sprintf (buf1, "%ld", tfcstats.day2[0]);
  100.     sprintf (buf2, "%ld", tfcstats.day2[1]);
  101.     tprintf ("  %24.24s: %-26.26s%-24.24s\n", "Traffic since midnight",
  102.         buf1, buf2);
  103.     sprintf (buf1, "%ld", tfcstats.hour2[0]);
  104.     sprintf (buf2, "%ld", tfcstats.hour2[1]);
  105.     tprintf ("  %24.24s: %-26.26s%-24.24s\n", "Traffic this hour",
  106.         buf1, buf2);
  107.     sprintf (buf1, "%d", (tfcstats.days > 1) ? (int)((tfcstats.messages2[0] - tfcstats.day2[0] + (tfcstats.days - 2)) / (tfcstats.days - 1)) : (int)tfcstats.messages2[0]);
  108.     sprintf (buf2, "%d", (tfcstats.days > 1) ? (int)((tfcstats.messages2[1] - tfcstats.day2[1] + (tfcstats.days - 2)) / (tfcstats.days - 1)) : (int)tfcstats.messages2[1]);
  109.     tprintf ("  %24.24s: %-26.26s%-24.24s\n\n", "Average traffic per day",
  110.         buf1, buf2);
  111.  
  112.     return 0;
  113. }
  114.  
  115.  
  116. int
  117. STATdaily_tfc(argc,argv,p)
  118. int argc OPTIONAL;
  119. char *argv[] OPTIONAL;
  120. void *p OPTIONAL;
  121. {
  122.     tprintf (dailyhdr, usagestr);
  123.     tprintf (tfcdailyhdr, STAT_emptystr,bulletinstr);
  124.     doGraph (24, tfcstats.hourly);
  125.     tputs (STAT_dailytrailer);
  126.     tprintf (tfcdailyhdr2, STAT_emptystr,ntsstr);
  127.     doGraph (24, tfcstats.hourly2);
  128.     tputs (STAT_dailytrailer);
  129.     return 0;
  130. }
  131.  
  132.  
  133. int
  134. STATweekly_tfc(argc,argv,p)
  135. int argc OPTIONAL;
  136. char *argv[] OPTIONAL;
  137. void *p OPTIONAL;
  138. {
  139.     tprintf (weeklyhdr, usagestr);
  140.     tprintf (tfcweeklyhdr, STAT_emptystr,bulletinstr);
  141.     doGraph (7, tfcstats.daily);
  142.     tputs (STAT_weeklytrailer);
  143.     tprintf (tfcweeklyhdr2, STAT_emptystr,ntsstr);
  144.     doGraph (7, tfcstats.daily2);
  145.     tputs (STAT_weeklytrailer);
  146.     return 0;
  147. }
  148.  
  149.  
  150. int
  151. STATmonthly_tfc(argc,argv,p)
  152. int argc OPTIONAL;
  153. char *argv[] OPTIONAL;
  154. void *p OPTIONAL;
  155. {
  156.     tprintf (monthlyhdr, usagestr);
  157.     tprintf (tfcmonthlyhdr, STAT_emptystr,bulletinstr);
  158.     doGraph (31, tfcstats.monthly);
  159.     tputs (STAT_monthlytrailer);
  160.     tprintf (tfcmonthlyhdr2, STAT_emptystr,ntsstr);
  161.     doGraph (31, tfcstats.monthly2);
  162.     tputs (STAT_monthlytrailer);
  163.     return 0;
  164. }
  165.  
  166.  
  167. int
  168. STATyearly_tfc(argc,argv,p)
  169. int argc OPTIONAL;
  170. char *argv[] OPTIONAL;
  171. void *p OPTIONAL;
  172. {
  173.     tprintf (yearlyhdr, usagestr);
  174.     tprintf (tfcyearlyhdr, STAT_emptystr,bulletinstr);
  175.     doGraph (12, tfcstats.yearly);
  176.     tputs (STAT_yearlytrailer);
  177.     tprintf (tfcyearlyhdr2, STAT_emptystr,ntsstr);
  178.     doGraph (12, tfcstats.yearly2);
  179.     tputs (STAT_yearlytrailer);
  180.     return 0;
  181. }
  182.  
  183.  
  184. void
  185. STATS_addtfc (int which, int amount)
  186. {
  187.     if (which < 0 || which > 3)
  188.         return;
  189.     if (which < 2)    {
  190.         which %= 2;
  191.         tfcstats.messages[which] += amount;
  192.         tfcstats.hour[which] += amount;
  193.         tfcstats.day[which] += amount;
  194.         tfcstats.month[which] += amount;
  195.     } else    {
  196.         which %= 2;
  197.         tfcstats.messages2[which] += amount;
  198.         tfcstats.hour2[which] += amount;
  199.         tfcstats.day2[which] += amount;
  200.         tfcstats.month2[which] += amount;
  201.     }
  202. }
  203.  
  204.  
  205. void
  206. updatestats_tfc (void)
  207. {
  208.     /* nothing, at this time - addtfcstats() does all the work */
  209. }
  210.  
  211.  
  212. void
  213. loadstats_tfc(void)
  214. {
  215. FILE *fp;
  216. char buffer[256];
  217.  
  218.     sprintf (buffer, "%s/traffic.dat", STATSDir);
  219.     fp = fopen (buffer, "r");
  220.     if (fp != NULLFILE)    {
  221.         (void) fread (&tfcstats, sizeof (struct tfc_stats), 1, fp);
  222.         (void) fclose (fp);
  223.     }
  224. }
  225.  
  226.  
  227. void
  228. savestats_tfc(void)
  229. {
  230. FILE *fp;
  231. char buffer[256];
  232.  
  233.     sprintf (buffer, "%s/traffic.dat", STATSDir);
  234.     fp = fopen (buffer, "w");
  235.     if (fp != NULLFILE)    {
  236.         fwrite (&tfcstats, sizeof (struct tfc_stats), 1, fp);
  237.         (void) fclose (fp);
  238.     } else
  239.         log (-1, "Can't open stats file '%s/traffic.dat'", STATSDir);
  240. }
  241.  
  242.  
  243. void
  244. newhour_tfc (int hour)
  245. {
  246.     tfcstats.hourly[hour][0] = tfcstats.hour[0];
  247.     tfcstats.hourly[hour][1] = tfcstats.hour[1];
  248.     tfcstats.hour[0] = tfcstats.hour[1] = 0;
  249.     tfcstats.hourly2[hour][0] = tfcstats.hour2[0];
  250.     tfcstats.hourly2[hour][1] = tfcstats.hour2[1];
  251.     tfcstats.hour2[0] = tfcstats.hour2[1] = 0;
  252. }
  253.  
  254.  
  255. void
  256. newday_tfc (int day)
  257. {
  258.     tfcstats.daily[day][0] = tfcstats.day[0];
  259.     tfcstats.daily[day][1] = tfcstats.day[1];
  260.     tfcstats.daily2[day][0] = tfcstats.day2[0];
  261.     tfcstats.daily2[day][1] = tfcstats.day2[1];
  262. }
  263.  
  264.  
  265. void
  266. endmonthclear_tfc (int day, int month)
  267. {
  268. int k;
  269. FILE *fp;
  270.  
  271.     /* clear out non-existent days of last month */
  272.     for (k = day; k < 31; k++)    {
  273.         tfcstats.monthly[k][0] = tfcstats.monthly[k][1] = 0L;
  274.         tfcstats.monthly2[k][0] = tfcstats.monthly2[k][1] = 0L;
  275.     }
  276.     tfcstats.yearly[month][0] = tfcstats.month[0];
  277.     tfcstats.yearly[month][1] = tfcstats.month[1];
  278.     tfcstats.yearly2[month][0] = tfcstats.month2[0];
  279.     tfcstats.yearly2[month][1] = tfcstats.month2[1];
  280.     fp = tmpfile();
  281.     if (fp)        {
  282.         fprintf (fp, "Monthly Traffic Stats for the month of %s: %s\n\n", Months[month], Hostname);
  283.         fprintf (fp, "Bulletins: %ld\nPersonal: %ld\nNTS: %ld\nForwarded: %ld\n",
  284.             tfcstats.month[0], tfcstats.month[1], tfcstats.month2[0], tfcstats.month2[1]);
  285.         rewind (fp);
  286.         (void) rdaemon (fp, NULLCHAR, NULLCHAR, "sysop", "Monthly Traffic Stats", 'P', 0);
  287.         (void) fclose (fp);
  288.     }
  289.     tfcstats.month[0] = tfcstats.month[1] = 0;
  290.     tfcstats.month2[0] = tfcstats.month2[1] = 0;
  291. }
  292.  
  293.  
  294. void
  295. endday_tfc (int day)
  296. {
  297.     tfcstats.monthly[day][0] = tfcstats.day[0];
  298.     tfcstats.monthly[day][1] = tfcstats.day[1];
  299.     tfcstats.day[0] = tfcstats.day[1] = 0;
  300.     tfcstats.monthly2[day][0] = tfcstats.day2[0];
  301.     tfcstats.monthly2[day][1] = tfcstats.day2[1];
  302.     tfcstats.day2[0] = tfcstats.day2[1] = 0;
  303.     tfcstats.days++;
  304. }
  305.  
  306.  
  307. void
  308. eachcycle_tfc (time_t now)
  309. {
  310.     tfcstats.last = now;
  311. }
  312.  
  313.  
  314. void
  315. init_tfc (time_t now)
  316. {
  317.     tfcstats.days = 1;
  318.     tfcstats.start = now;
  319. }
  320.  
  321.  
  322. #endif /* STATS_TFC */
  323.